package org.geom;

public class Vector3D extends Point3D {

	public Vector3D() {
		super();
	}
	
	public Vector3D(float x, float y, float z) {
		super(x, y, z);
	}
	
	
	public final float dot(Vector3D v) {
		return x * v.x + y * v.y + z * v.z;
	}

	public Vector3D normalize() {
		Vector3D ret = new Vector3D(x, y, z);
		float mag = (float) Math.sqrt(x*x + y*y + z*z);
		
		if (mag > 0) {
			mag = 1f / mag;
			ret.x *= mag;
			ret.y *= mag;
			ret.z *= mag;
		} 
		return ret;
	}
	
	public static Vector3D crossProduct(Vector3D n, Vector3D m) {
		/*
		 * a * b * sin(theta)
		 * theta = angle between two vectors
		 * 
		 * */
		Vector3D ret = new Vector3D();
		Vector3D a = n;
		Vector3D b = m;
		
		/*float theta = (float)Math.acos(a.dot(b));
		float sinTheta = Maths.sin(theta);
		
		ret.x = a.x * b.x * sinTheta;
		ret.y = a.y * b.y * sinTheta;
		ret.z = a.z * b.z * sinTheta;*/
		
		ret.x = a.y * b.z - a.z * b.y;
		ret.x = n.y * m.z - m.y * n.z;
		
		ret.y = a.z * b.x - b.z * a.x;
		ret.y = n.z * m.x - m.z * n.x;
		
		ret.z = a.x * b.y - a.y * b.x;
		ret.z = n.x * m.y - n.y * m.x;
		
		//return ret;
		
           return ret;
      
	}
}
